﻿// Copyright (C) 2009  Lex Li
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

/*
 * Created by SharpDevelop.
 * Date: 10/9/2009
 * Time: 7:40 PM
 */

using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Lextm.M8Pack.Components;
using Lextm.M8Pack.ThemeBuilder.Properties;
using WeifenLuo.WinFormsUI.Docking;

namespace Lextm.M8Pack.ThemeBuilder
{
	/// <summary>
	/// Description of MainScreen.
	/// </summary>
	internal partial class MainScreen : DockContent
	{
		private readonly IList<AppIcon> _icons = new List<AppIcon>();
		private readonly Theme _theme;
	    private int _count;

	    public MainScreen(Theme theme, int index)
		{
	        _theme = theme;
	        InitializeComponent();
			foreach (Control c in Controls)
			{
				if (c.GetType() == typeof(AppIcon))
				{
					AppIcon icon = (AppIcon)c;
					icon.ShortcutClicked += IconShortcutClicked;
					_icons.Insert(0, icon);
				}
			}

            using (Image b = Helper.LoadImage(Theme.BackgroundImage))
            {
                phoneScreen1.Background = Helper.ShrinkBackground(b);
            }

            TabText = Resources.MainScreenTitle;
            // phoneScreen1.Locked = index != 1;
		}

	    private Theme Theme
		{
		    get { return _theme; }
		}

	    public bool Full
	    {
	        get { return _count == _icons.Count; }
	    }

	    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
		private void PhoneScreen1ShortcutClicked(object sender, ImageBrowsedEventArgs e)
		{
			if (Theme == null)
			{
				return;
			}
			
			using(Image image = Helper.LoadImage(e.FileName))
			{
				if (image.Width != 480 || image.Height != 720)
				{
					MessageBox.Show("Background must be a 480*720 JPEG image");
					return;
				}
				
				Theme.ReplaceBackground(e.FileName);
				phoneScreen1.Background = Helper.ShrinkBackground(image);
			}
			
			Repaint();
		}
		
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
		private void IconShortcutClicked(object sender, ImageBrowsedEventArgs e)
		{
			if (Theme == null)
			{
				return;
			}

            using (Image image = Helper.LoadImage(e.FileName))
			{
				if (image.Width < 90 || image.Height < 90)
				{
					MessageBox.Show("Application icon must be at least a 90*90 PNG image");
					return;
				}
				
				AppIcon icon = (AppIcon)sender;
				Theme.ReplaceIcon(icon.LabelName, e.FileName);
				icon.Image = Helper.ShrinkIcon(image);
				Repaint();
			}
		}
		
		private void Repaint()
		{
			phoneScreen1.Invalidate();
			foreach (AppIcon icon in _icons)
			{
				icon.Invalidate();
			}
		}

	    public Bitmap GetPreview()
	    {
	        Bitmap bitmap = new Bitmap(480, 720);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                phoneScreen1.OnDraw(g);
                foreach (AppIcon icon in _icons)
                {
                    icon.DrawOn(g, icon.Left, icon.Top);
                }
            }

	        return bitmap;
	    }

	    public void AddIcon(KeyValuePair<string, string> icon, string display)
	    {
            if (Full)
            {
                return;
            }

	        _icons[_count].DisplayName = display;
	        _icons[_count].LabelName = icon.Key;
            using (Image c = Helper.LoadImage(Theme.GetFile(icon.Value)))
            {
                _icons[_count].Image = Helper.ShrinkIcon(c);
            }

	        _count++;
	    }

	    public void ChangeFont(Font font)
	    {
	        foreach (AppIcon icon in _icons)
	        {
	            icon.LabelFont = font.Name;
	        }

            Repaint();
	    }

	    public void SwitchMode()
	    {
	        foreach (AppIcon icon in _icons)
	        {
	            icon.SwitchMode();
	        }

	        phoneScreen1.SwitchMode();
            Repaint();
	    }
	}
}
